package manager;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cs236369.hw5.SearchWSSearchWSSOAP11Port_httpStub;
import cs236369.hw5.SearchWSSearchWSSOAP11Port_httpStub.Search;
import cs236369.hw5.SearchWSSearchWSSOAP11Port_httpStub.SearchResponse;

/**
 * Servlet implementation class SearchOnWS
 */
public class SearchOnWS extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchOnWS() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String WSName = request.getParameter("ws_name");
		String keywordsString = request.getParameter("keywords");
		
		if (WSName == null || keywordsString == null) {
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Missing WS Name/keywords");
			return;
		}
		
		try {
		
			SearchWSSearchWSSOAP11Port_httpStub stub = new SearchWSSearchWSSOAP11Port_httpStub(
					"http://132.68.39.145:80/" + WSName + "/services/SearchWS");
			
//			SearchWSSearchWSSOAP11Port_httpStub stub = new SearchWSSearchWSSOAP11Port_httpStub(
//					"http://localhost:8080/www_hw5/services/SearchWS");
			
			String[] keywords = keywordsString.split(" ");
						
			Search search = new Search();
			search.setKeywords(keywords);
			
			SearchResponse searchResponse = stub.search(search);
			
			String[] res = searchResponse.get_return();
			if (res == null) {
				response.getWriter().println("No Results!");
				return;
			}
			
			int allRes = 0;
			for(int i = 0; i < res.length; i++) {
				if (res[i] == null) {
					continue;
				}
				
				if (res[i].contains("||")) {
				
					String courseName = res[i].substring(0, res[i].indexOf("||"));
					String courseDetails = res[i].substring(res[i].indexOf("||") + 2, res[i].length());
					
					response.getWriter().println("Course Name: " + courseName.replaceAll("\n", "<br />"));
					response.getWriter().println("<br />");
					response.getWriter().println("Details: " + courseDetails.replaceAll("\n", "<br />"));
				}
				else {
					response.getWriter().println(res[i].replaceAll("\n", "<br />"));
				}
				
				response.getWriter().println("<ENDOFCOURSE>");
				allRes++;
			}
			
			if (allRes == 0) {
				response.getWriter().println("No Results!");
				return;
			}
		} catch (Exception e) {
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		} // do nothing if the WS fails
			
		
				
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
